const format = time => {
    const date = new Date(time)

    const year = date.getFullYear()
    const month = date.getMonth() + 1 // 月份是从0开始的
    const day = date.getDate()
    const hour = date.getHours()
    const min = date.getMinutes()
    const sec = date.getSeconds()
    const newTime =
        year +
        '-' +
        (month < 10 ? '0' + month : month) +
        '-' +
        (day < 10 ? '0' + day : day) +
        ' ' +
        (hour < 10 ? '0' + hour : hour) +
        ':' +
        (min < 10 ? '0' + min : min) +
        ':' +
        (sec < 10 ? '0' + sec : sec)

    return newTime
}


function resetSearchForm() {
    for (let key in this.search) {
        if (key == 'size') {
            this.search[key] = 10
        } else if (key == 'current') {
            this.search[key] = 1
        } else {
            this.search[key] = null
        }
    }
}

function onReset() {
    this.resetSearchForm();
    this.onSubmit();
}


function hasPrivilege(code) {
    if (this.$store.state.userPrivilege.indexOf(code) != -1) {
        return true;
    } else {
        return false;
    }
}


function handleSizeChange(val) {
    this.search.size = val;
    this.search.current = 1;
    //分页大小改变
    this.onSubmit();
}

function handleCurrentChange(val) {
    //分页页码改变需要重新提交表单
    this.search.current = val
    this.onSubmit();
}


// 确认操作 点击确定执行confirmAction方法 点击取消则不执行
function confirmOperation(message, confirmAction, actionParams) {
    this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        confirmAction(actionParams)
    }).catch(() => {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });
    });
}



export default {
    methods: {
        //初始化表单
        resetSearchForm,
        //格式化时间 
        format,
        //判断用户是否具有权限
        hasPrivilege,
        //分页大小变化,翻页重新提交表单
        handleSizeChange,
        handleCurrentChange,
        //重置搜索条件,重新提交表单
        onReset,
        confirmOperation
    }
}


